# ！ /usr/bin/python3
# -*- coding:utf-8 -*-
# @Author:Peng Cao
# @File: locust01.py
# @Software: PyCharm
import requests
from locust import User, TaskSet, task, HttpUser, constant, SequentialTaskSet
import re
import sys
import os
import time
import queue


# @task
# def indexout(user):
#     res = user.client.request(method='GET', url='/s?wd=python', name='查询python')
#     print(res)

class UserBehavior1(SequentialTaskSet):
    """
    有序执行任务
    """

    @task
    def test_index(self):
        # res = self.client.request(method='GET', url='/s?wd=python', name='查询python')
        res = self.client.get(url='/s?wd=python', name='有序执行任务，查询python')
        print('有序执行任务，查询python')

    @task(10)
    def login(self):
        res = self.client.get(url='/s?wd=java', name='有序执行任务，查询java')
        print('有序执行任务，查询java')


class UserBehavior2(TaskSet):
    """
    并行执行任务
    """

    print("开始运行-------12")
    @task
    def test_index(self):
        # res = self.client.request(method='GET', url='/s?wd=python', name='查询python')
        res = self.client.get(url='/s?wd=python', name='并行执行任务，查询python')
        print('并行执行任务，查询python')

    # @task
    def login(self):
        res = self.client.get(url='/s?wd=java', name='并行执行任务，查询java')
        print('并行执行任务，查询java')


class WebSiteUser(HttpUser):
    print("开始运行10")
    host = "https://www.baidu.com"
    wait_time = constant(3)  # 每次请求的间隔时间
    tasks = [UserBehavior2]
    print("开始运行11")
    # tasks = [indexout]

    # @task
    # def test_index(self):
    #     res = self.client.get('/s?wd=python')
    #     print(res)
    #     res = self.client.request(method='GET', url='/s?wd=python', name='查询python')
    #     print(res)


if __name__ == '__main__':
    current_file_path = os.path.abspath(__file__)
    os.system(f"locust -f {current_file_path}")
    # os.system(f"locust -f {current_file_path} --master")
    # os.system("locust -f locust01.py --host=https://www.cnblogs.com")
